package com.ruoyi.dishes.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dishes.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.dishes.domain.DishesType;
import com.ruoyi.dishes.service.IDishesTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 菜品分类Controller
 * 
 * @author ruoyi
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/dishesType")
public class DishesTypeController extends BaseController
{
    @Autowired
    private IDishesTypeService dishesTypeService;

    /**
     * 查询菜品分类列表
     */
    @RequiresPermissions("dishes:dishesType:list")
    @GetMapping("/list")
    public TableDataInfo list(DishesType dishesType)
    {
        startPage();
        List<DishesType> list = dishesTypeService.selectDishesTypeList(dishesType);
        return getDataTable(list);
    }

    /**
     * 导出菜品分类列表
     */
    @RequiresPermissions("dishes:dishesType:export")
    @Log(title = "菜品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DishesType dishesType)
    {
        List<DishesType> list = dishesTypeService.selectDishesTypeList(dishesType);
        ExcelUtil<DishesType> util = new ExcelUtil<DishesType>(DishesType.class);
        util.exportExcel(response, list, "菜品分类数据");
    }

    /**
     * 获取菜品分类详细信息
     */
    @RequiresPermissions("dishes:dishesType:query")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return success(dishesTypeService.selectDishesTypeByTypeId(typeId));
    }

    /**
     * 新增菜品分类
     */
    @RequiresPermissions("dishes:dishesType:add")
    @Log(title = "菜品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DishesType dishesType)
    {
        IdWorker idWorker = new IdWorker();
        dishesType.setTypeId(idWorker.nextId());
        return toAjax(dishesTypeService.insertDishesType(dishesType));
    }

    /**
     * 修改菜品分类
     */
    @RequiresPermissions("dishes:dishesType:edit")
    @Log(title = "菜品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DishesType dishesType)
    {
        return toAjax(dishesTypeService.updateDishesType(dishesType));
    }

    /**
     * 删除菜品分类
     */
    @RequiresPermissions("dishes:dishesType:remove")
    @Log(title = "菜品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds)
    {
        return toAjax(dishesTypeService.deleteDishesTypeByTypeIds(typeIds));
    }
}
